package com.itheima.servlet;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class SpringIOC {

    /**
     * spring 提供的IOC
     * @param args
     */
    public static void main(String[] args) {
        /**
         * ApplicationContext 三个实现类
         *  ClassPathXmlApplicationContext  加载类路径下的配置文件
         *  FileSystemXmlApplicationContext  磁盘任意位置下的文件(文件权限)
         *  AnnotationConfigApplicationContext 读取注解创建容器
         *
         * ApplicationContext 立即加载  单例
         * BeanFactory  延迟加载  多个实例

         */
        //-----------:ApplicationContext:------------
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean_annotation.xml");
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        UserService userService = applicationContext.getBean("userService", UserService.class);
        System.out.println(userDao);
        System.out.println(userService);



        // -----------:BeanFactory:----------------
        BeanFactory beanFactory = null;
        Resource resource = new ClassPathResource("bean_annotation.xml");
        beanFactory = new XmlBeanFactory(resource);
        UserService userService1 = (UserService) beanFactory.getBean("userService");
        System.out.println(userService1);
    }
}
